package com.example.smarterhouse;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	Controller c;
	private Button wifi, internet, login;
	private EditText user, password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		c = Controller.getInstance();
		wifi = (Button) findViewById(R.id.Wifi);
		internet = (Button) findViewById(R.id.Internet);
		login = (Button) findViewById(R.id.Login);
		user = (EditText) findViewById(R.id.Username);
		password = (EditText) findViewById(R.id.Password);
		login.setVisibility(View.GONE);
		user.setVisibility(View.GONE);
		password.setVisibility(View.GONE);
		login.setOnClickListener(this);
		user.setOnClickListener(this);
		password.setOnClickListener(this);
		wifi.setOnClickListener(this);
		internet.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String connectionMethod = "";
		if (v.equals(internet)) {
			connectionMethod = "inter";
			c.setLoginMethod(connectionMethod);
		}else if(v.equals(wifi)){
			connectionMethod = "wifi";
			c.setLoginMethod(connectionMethod);

		}
		wifi.setVisibility(View.GONE);
		internet.setVisibility(View.GONE);
		user.setVisibility(View.VISIBLE);
		password.setVisibility(View.VISIBLE);
		login.setVisibility(View.VISIBLE);
		
		c.setCon(this);
		c.setClassDest(RoomAndDeviceList.class);
		
		//**********************************************
		//Added By Sanja & Henrik for the async call
		//**********************************************

		if (v.getId() == R.id.Login) {
			String login = "authenticate_"+user.getText().toString() + "_"+password.getText().toString()+"_";
			
			if(!c.callAsync(login)){
				Toast toast = Toast.makeText(getApplicationContext(),  "Login failed please try again", Toast.LENGTH_LONG);
				toast.show();
			}
			
//			c.connect(userName, pw);
		}
			}
	
	
	
}
